package com.hp.note;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.os.Environment;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;



public class AudioActivity extends Activity
{
    private static final String LOG_TAG = "AudioRecord";
    private static String mFileName = null;

    private Button mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private Button   mPlayButton = null;
    private MediaPlayer   mPlayer = null;
    
    boolean mStartPlaying = true;
    boolean mStartRecording = true;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    public AudioActivity() {
    	
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
		setContentView(R.layout.activity_audio);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    	String currentDateandTime = sdf.format(new Date());
    	
    	Log.i("audioactivity", "started");
        mFileName = getApplicationContext().getFilesDir ().getAbsolutePath();
        mFileName += "/" + currentDateandTime + ".aac";
//    	mFileName = currentDateandTime + ".aac";
        Log.i("filename", mFileName);
		
		mPlayButton = (Button) this.findViewById(R.id.btn_play);
		mPlayButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				onPlay(mStartPlaying);
                if (mStartPlaying) {
                    mPlayButton.setText("Stop");
                    Log.i("audioactivity", "startplay");
                } else {
                	mPlayButton.setText("Play");
                	Log.i("audioactivity", "stopplay");
                }
                mStartPlaying = !mStartPlaying;
			}
		});
		
		mRecordButton = (Button) this.findViewById(R.id.btn_record);
		mRecordButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onRecord(mStartRecording);
                if (mStartRecording) {
                    mRecordButton.setText("Stop");
                    Log.i("audioactivity", "startrecord");
                } else {
                	mRecordButton.setText("Record");
                	Log.i("audioactivity", "stoprecord");
                }
                mStartRecording = !mStartRecording;
			}
		});
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}